import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class UserInfo {
  String name = "leo";
  int age = 10;
  UserInfo(this.name, this.age);
}

/// with ChangeNotifier
class MyCounter with ChangeNotifier {
  final UserInfo _userInfo = UserInfo("leo", 10);
  UserInfo get userInfo => _userInfo;

  void add() {
    _userInfo.age++;
    notifyListeners();
  }
}

///  extends ChangeNotifier
class MySubtract extends ChangeNotifier {
  final UserInfo _userInfo = UserInfo("jim", 100);
  UserInfo get userInfo => _userInfo;

  void sub() {
    _userInfo.age--;
    notifyListeners();
  }
}

class ProviderDemo extends StatelessWidget {
  const ProviderDemo({super.key});

  @override
  Widget build(BuildContext context) {
    print('ProviderDemo build-----');

    return MaterialApp(
      title: 'Flutter widget',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    print('MyHomePage build-----');

    return Scaffold(
      appBar: AppBar(
        title: const Text("Flutter Provider"),
      ),
      body: const Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            MyContainer1(),
            SizedBox(height: 30),
            MyContainer2()
          ],
        ),
      ),
    );
  }
}

class MyContainer1 extends StatefulWidget {
  const MyContainer1({super.key});

  @override
  State<MyContainer1> createState() => _MyContainer1State();
}

class _MyContainer1State extends State<MyContainer1> {
  @override
  Widget build(BuildContext context) {
    print('MyContainer1 build-----');

    final String name1 = Provider.of<MyCounter>(context).userInfo.name;
    final int age1 = Provider.of<MyCounter>(context).userInfo.age;

    final String name2 = Provider.of<MySubtract>(context).userInfo.name;
    final int age2 = Provider.of<MySubtract>(context).userInfo.age;

    return Column(
      children: <Widget>[
        Text("Container1 name1=$name1  age1=$age1", style: myTextStyle1()),
        Text("Container1 name2=$name2  age2=$age2", style: myTextStyle2()),
      ],
    );
  }
}

TextStyle myTextStyle1() {
  return const TextStyle(fontSize: 20, color: Colors.blue);
}

TextStyle myTextStyle2() {
  return const TextStyle(fontSize: 20, color: Colors.green);
}

class MyContainer2 extends StatefulWidget {
  const MyContainer2({super.key});

  @override
  State<MyContainer2> createState() => _MyContainer2State();
}

class _MyContainer2State extends State<MyContainer2> {
  @override
  Widget build(BuildContext context) {
    print('_MyContainer2State build-----');
    return Container(
      child: Consumer2<MyCounter, MySubtract>(
        builder: (ctx, counterVM, subtractVM, child) {
          print('_MyContainer2State Consumer2  build-----');
          return Column(
            children: <Widget>[
              Text(
                  "Container2 name1=${counterVM.userInfo.name}  age1=${counterVM.userInfo.age}",
                  style: myTextStyle1()),
              Text(
                  "Container2 name2=${subtractVM.userInfo.name}  age2=${subtractVM.userInfo.age}",
                  style: myTextStyle2()),
              const SizedBox(
                height: 30,
              ),
              TextButton(
                child: const Text("点 击"),
                onPressed: () {
                  counterVM.add();
                  subtractVM.sub();
                },
              ),
            ],
          );
        },
      ),
    );
  }
}
